/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package todonet;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.tree.TreeNode;
import org.joda.time.Period;
import org.joda.time.format.PeriodFormatter;

/**
 *
 * @author DNS
 */
public class ToDoTask implements TreeNode {

    public ToDoTask(String title) {
        setTitle(title);
        //setDateOfEnd(new Date(2011, 2, 13));
    }

    public ToDoTask() {
    }

    @Override
    public String toString() {
        String title =  '[' + String.format("%1$3d%2$s", getPercentComplete() ,'%') + "] " +_title;
//        if (getDateOfEnd() != null)
//            title += "[ elapsed at " + getDateOfEnd().toString() + "]";
//        else
//            title += "[date of end is NULL]";
        return title;
    }

    public List<ToDoTask> getChilds() {
        return _childs;
    }

    public void setChilds(List<ToDoTask> _childs) {
        this._childs = _childs;
    }

    public Date getDateOfEnd() {
        return _dateOfEnd;
    }

    public void setDateOfEnd(Date _dateOfEnd) {
        this._dateOfEnd = _dateOfEnd;
    }

    public int getImportant() {
        return _important;
    }

    public void setImportant(int _important) {
        this._important = _important;
    }

    public ToDoTask getParent() {
        return _parent;
    }

    public void setParent(ToDoTask _parent) {
        this._parent = _parent;
    }

    public byte getPercentComplete() {
        long totalPercent = _percentComplete;
        for (ToDoTask t : getChilds())
            totalPercent += t.getPercentComplete();
        return (byte) (totalPercent / (getChilds().size() + 1));
    }

    public byte getSelfPercentComplete() {
        return _percentComplete;
    }

    public void setPercentComplete(byte _percentComplete) {
        if (_percentComplete > 100)
            _percentComplete = 100;
        this._percentComplete = _percentComplete;
    }

    public String getTitle() {
        return _title;
    }

    public void setTitle(String _title) {
        this._title = _title;
    }

    private ToDoTask _parent = null;
    private List<ToDoTask> _childs = new ArrayList<ToDoTask>();
    private String _title = "";
    private int _important = 1;
    private byte _percentComplete = 0;
    private Date _dateOfEnd = null;

    public long getRemainDays()
    {
        if (_dateOfEnd == null)
            return 0;
        Period p = new Period(System.currentTimeMillis(), _dateOfEnd.getTime());
        return p.getDays();
    }

    public String getRemainTimeString()
    {
        if (_dateOfEnd == null)
            return "";
        if (System.currentTimeMillis() > _dateOfEnd.getTime())
            return "Задача просрочена";
        Period p = new Period(System.currentTimeMillis(), _dateOfEnd.getTime());
        String timeString = "";
        if (p.getYears() > 0)
            timeString += p.getYears()+" г. ";
        if (p.getMonths() > 0)
            timeString += p.getMonths()+" мес. ";
        if (p.getDays() > 0)
            timeString += (p.getWeeks() * 7 + p.getDays()) + " д. ";
        if (p.getHours() > 0)
            timeString += p.getHours()+" ч. ";
        if (timeString.equals(""))
            timeString = " менее часа";
        return "До окончания осталось: " + timeString;
    }

    public TreeNode getChildAt(int childIndex) {
        return getChilds().get(childIndex);
    }

    public int getChildCount() {
        return getChilds().size();
    }

    public int getIndex(TreeNode node) {
        return getChilds().indexOf(node);
    }

    public boolean getAllowsChildren() {
        return !getChilds().isEmpty();
    }

    public boolean isLeaf() {
        return getChilds().isEmpty();
    }

    public Enumeration children() {
        return (Enumeration) getChilds();
    }
}
